import 'dart:io';

import 'package:yaml/yaml.dart';

class CommandUtil {
  CommandUtil._();

  /// 加载配置文件
  static YamlMap loadConfig(String p) {
    File configFile = File('${Directory.current.path}/$p');
    String content = "";

    /// 未指定配置文件
    if (!configFile.existsSync()) {
      print('配置文件不存在，将使用默认配置');
      return YamlMap();
    }

    /// 配置文件读取为空
    content = configFile.readAsStringSync();
    if (content.isEmpty) {
      print('配置文件为空，将使用默认配置');

      return YamlMap();
    }
    final yaml = loadYaml(content);
    return yaml;
  }

  static void formatFile(List<String> files) async {
    final args = ['format'] + files;
    await Process.start('dart', args);
  }

  static final Map<String, String> _keyWords = {};

  static Map<String, String> jsonKeys() {
    return _keyWords;
  }

  static void addKeywords(YamlList? keys) {
    if (keys == null || keys.isEmpty) {
      return;
    }
    _keyWords.clear();
    for (var element in keys.value) {
      if (element is YamlMap) {
        final maps = element.keys.map((e) => MapEntry(e.toString(), element[e].toString()));
        _keyWords.addEntries(maps);
      }
    }
  }

  /// 运行参数
  static List<String> arguments = [];
}
